//
//  LFUtilit.m
//  DPGJ
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-11-18.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "LFUtilit.h"
#import "CONSTS.h"

void showAlert(NSString *title_, NSString *Message)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: title_ 
                                                    message: Message 
                                                   delegate: nil 
                                          cancelButtonTitle: @"确定" 
                                          otherButtonTitles: nil];
    [alert show];
    [alert release];
}

BOOL verifyServerRetunSuccess(NSDictionary *returnDic)
{
    if (!returnDic)
    {
        return NO;
    }
    
    id dic = [returnDic objectForKey: @"error_response"];
    if (dic)
    {
        return NO;
    }
    
    return YES;
}

NSString *getOrderStatusByStr(NSString *str)
{
    /*
     交易状态。可选值: 
     * TRADE_NO_CREATE_PAY(没有创建支付宝交易) 
     
     * WAIT_BUYER_PAY(等待买家付款) 
     
     * WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
     
     * WAIT_BUYER_CONFIRM_GOODS(等待买家确认收货,即:卖家已发货)
     
     * TRADE_BUYER_SIGNED(买家已签收,货到付款专用)
     
     * TRADE_FINISHED(交易成功) 
     
     * TRADE_CLOSED(付款以后用户退款成功，交易自动关闭) 
     
     * TRADE_CLOSED_BY_TAOBAO(付款以前，卖家或买家主动关闭交易)
     
     */
    
    NSString *statusOfOrder = @"未知";
    
    if ([str isEqualToString: @"TRADE_NO_CREATE_PAY"]) 
    {
        // * TRADE_NO_CREATE_PAY(没有创建支付宝交易)
        statusOfOrder = TRADE_NO_CREATE_PAY;
    }else if ([str isEqualToString: @"WAIT_BUYER_PAY"])
    {
        //* WAIT_BUYER_PAY(等待买家付款) 
        statusOfOrder = WAIT_BUYER_PAY;
    }else if ([str isEqualToString: @"WAIT_SELLER_SEND_GOODS"])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        statusOfOrder = WAIT_SELLER_SEND_GOODS;
    }else if ([str isEqualToString: @"WAIT_BUYER_CONFIRM_GOODS"])
    {
        //* WAIT_BUYER_CONFIRM_GOODS(等待买家确认收货,即:卖家已发货)
        statusOfOrder = WAIT_BUYER_CONFIRM_GOODS;
    }else if ([str isEqualToString: @"TRADE_BUYER_SIGNED"])
    {
        //* TRADE_BUYER_SIGNED(买家已签收,货到付款专用)
        statusOfOrder = TRADE_BUYER_SIGNED;
    }else if ([str isEqualToString: @"TRADE_FINISHED"])
    {
        //* TRADE_FINISHED(交易成功) 
        statusOfOrder = TRADE_FINISHED;
    }else if ([str isEqualToString: @"TRADE_CLOSED"])
    {
        //* TRADE_CLOSED(付款以后用户退款成功，交易自动关闭) 
        statusOfOrder = TRADE_CLOSED;
    }else if ([str isEqualToString: @"TRADE_CLOSED_BY_TAOBAO"])
    {
        //* TRADE_CLOSED_BY_TAOBAO(付款以前，卖家或买家主动关闭交易)
        statusOfOrder = TRADE_CLOSED_BY_TAOBAO;
    }
    
    return statusOfOrder;
}

NSString *getOrderStatusStrByStatus(NSString *str)
{
    /*
     交易状态。可选值: 
     * TRADE_NO_CREATE_PAY(没有创建支付宝交易) 
     
     * WAIT_BUYER_PAY(等待买家付款) 
     
     * WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
     
     * WAIT_BUYER_CONFIRM_GOODS(等待买家确认收货,即:卖家已发货)
     
     * TRADE_BUYER_SIGNED(买家已签收,货到付款专用)
     
     * TRADE_FINISHED(交易成功) 
     
     * TRADE_CLOSED(付款以后用户退款成功，交易自动关闭) 
     
     * TRADE_CLOSED_BY_TAOBAO(付款以前，卖家或买家主动关闭交易)
     
     */
    
    NSString *statusOfOrder = @"未知";
    
    if ([str isEqualToString: TRADE_NO_CREATE_PAY]) 
    {
        // * TRADE_NO_CREATE_PAY(没有创建支付宝交易)
        statusOfOrder = @"TRADE_NO_CREATE_PAY";
    }else if ([str isEqualToString: WAIT_BUYER_PAY])
    {
        //* WAIT_BUYER_PAY(等待买家付款) 
        statusOfOrder = @"WAIT_BUYER_PAY";
    }else if ([str isEqualToString: WAIT_SELLER_SEND_GOODS])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        statusOfOrder = @"WAIT_SELLER_SEND_GOODS";
    }else if ([str isEqualToString: WAIT_BUYER_CONFIRM_GOODS])
    {
        //* WAIT_BUYER_CONFIRM_GOODS(等待买家确认收货,即:卖家已发货)
        statusOfOrder = @"WAIT_BUYER_CONFIRM_GOODS";
    }else if ([str isEqualToString: TRADE_BUYER_SIGNED])
    {
        //* TRADE_BUYER_SIGNED(买家已签收,货到付款专用)
        statusOfOrder = @"TRADE_BUYER_SIGNED";
    }else if ([str isEqualToString: TRADE_FINISHED])
    {
        //* TRADE_FINISHED(交易成功) 
        statusOfOrder = @"TRADE_FINISHED";
    }else if ([str isEqualToString: TRADE_CLOSED])
    {
        //* TRADE_CLOSED(付款以后用户退款成功，交易自动关闭) 
        statusOfOrder = @"TRADE_CLOSED";
    }else if ([str isEqualToString: TRADE_CLOSED_BY_TAOBAO])
    {
        //* TRADE_CLOSED_BY_TAOBAO(付款以前，卖家或买家主动关闭交易)
        statusOfOrder = @"TRADE_CLOSED_BY_TAOBAO";
    }
    
    return statusOfOrder;
}

NSString *getOrderTypeByStr(NSString *str)
{
    /*
     #define                                            @"一口价"
     #define                                          @"拍卖" 
     #define                                  @"一口价、拍卖"
     #define                         @"旺店入门版交易"
     #define                           @"旺店标准版交易" 
     #define                                    @"自动发货"
     #define                                               @"直冲" 
     #define                                              @"货到付款"
     #define                                   @"游戏装备"
     #define                                     @"ShopEX交易"
     #define                                      @"万网交易"
     #define                                   @"统一外部交易"
     #define                                    @"即时到账"
     #define                                          @"大商家货到付款"
     #define                                      @"酒店类型交易"
     #define                               @"商超交易"
     #define                           @"商超货到付款交易"
     */
    NSString *type = nil;
    if ([str isEqualToString: fixed]) 
    {
        // * TRADE_NO_CREATE_PAY(没有创建支付宝交易)
        type = @"fixed";
    }else if ([str isEqualToString: auction])
    {
        //* WAIT_BUYER_PAY(等待买家付款) 
        type = @"auction";
    }else if ([str isEqualToString: guarantee_trade])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"guarantee_trade";
    }else if ([str isEqualToString: independent_simple_trade])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"independent_simple_trade";
    }else if ([str isEqualToString: independent_shop_trade])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"independent_shop_trade";
    }else if ([str isEqualToString: auto_delivery])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"auto_delivery";
    }else if ([str isEqualToString: ec])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"ec";
    }else if ([str isEqualToString: cod])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"cod";
    }else if ([str isEqualToString: game_equipment])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"game_equipment";
    }else if ([str isEqualToString: shopex_trade])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"shopex_trade";
    }else if ([str isEqualToString: netcn_trade])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"netcn_trade";
    }else if ([str isEqualToString: external_trade])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"external_trade";
    }else if ([str isEqualToString: instant_trade])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"instant_trade";
    }else if ([str isEqualToString: b2c_cod])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"b2c_cod";
    }else if ([str isEqualToString: hotel_trade])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"hotel_trade";
    }else if ([str isEqualToString: super_market_trade])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"super_market_trade";
    }else if ([str isEqualToString: super_market_cod_trade])
    {
        //* WAIT_SELLER_SEND_GOODS(等待卖家发货,即:买家已付款)
        type = @"super_market_cod_trade";
    }else
    {
        type = nil;
    }
    
    return type;
}

NSString *getRateStatusBy(NSString *rateStatusStr)
{
    /*
     #define RATE_UNBUYER                @"买家未评"
     
     #define RATE_UNSELLER               @"卖家未评"
     
     #define RATE_BUYER_UNSELLER         @"买家已评，卖家未评"
     
     #define RATE_UNBUYER_SELLER         @"买家未评，卖家已评"
     */
    NSString *rate_status = nil;
    
    if ([rateStatusStr isEqualToString: RATE_UNBUYER]) 
    {
        // * TRADE_NO_CREATE_PAY(没有创建支付宝交易)
        rate_status = @"RATE_UNBUYER";
    }else if ([rateStatusStr isEqualToString: RATE_UNSELLER])
    {
        //* WAIT_BUYER_PAY(等待买家付款) 
        rate_status = @"RATE_UNSELLER";
    }else if ([rateStatusStr isEqualToString: RATE_BUYER_UNSELLER])
    {
        //* WAIT_BUYER_PAY(等待买家付款) 
        rate_status = @"RATE_BUYER_UNSELLER";
    }else if ([rateStatusStr isEqualToString: RATE_UNBUYER_SELLER])
    {
        //* WAIT_BUYER_PAY(等待买家付款) 
        rate_status = @"RATE_UNBUYER_SELLER";
    }else
    {
        rate_status = nil;
    }
    
    return rate_status;
}

void writeJsonToPlist(NSDictionary *dic, NSString *fileName)
{
#ifdef DEBUG
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    fileName = [NSString stringWithFormat: @"%@.plist", fileName];
    NSString *path = [documentsDirectory stringByAppendingPathComponent: fileName];
    ITTDINFO(@"%@", path);
    
    [dic writeToFile: path atomically: YES];
#endif
}

CGSize bubbleView(NSString *text, UIFont *font, float width)
{
	//CGSize size = [text sizeWithFont: font forWidth: 300 lineBreakMode: UILineBreakModeCharacterWrap];
    ITTDINFO(@"text = %@\nwidth = %f", text, width);
    CGSize size = [text sizeWithFont: font constrainedToSize: CGSizeMake(width, 1000) lineBreakMode:UILineBreakModeCharacterWrap];
    
	return size;
}

void disSelctCell(UITableView *tableView_)
{
    NSIndexPath *indexPath = [tableView_ indexPathForSelectedRow];
    
    [tableView_ deselectRowAtIndexPath: indexPath animated: YES];
}

void setCellLabelFrame(UIView **view_, float height_)
{
    (*view_).frame = CGRectMake((*view_).frame.origin.x,
                                (*view_).frame.origin.y + height_,
                                (*view_).frame.size.width, 
                                (*view_).frame.size.height);
}

//通过标记获取邮递方式
NSString *getSendKindWithSign(NSString *yunSongFangShi)
{
    //物流方式.可选值:free(卖家包邮),post(平邮),express(快递),ems(EMS).
    if ([yunSongFangShi isEqualToString: @"ems"]) 
    {
        yunSongFangShi = @"ems快递";
    }else if ([yunSongFangShi isEqualToString: @"express"])
    {
        yunSongFangShi = @"快递";
    }else if ([yunSongFangShi isEqualToString: @"post"])
    {
        yunSongFangShi = @"平邮";
    }else if ([yunSongFangShi isEqualToString: @"free"])
    {
        yunSongFangShi = @"卖家包邮";
    }else
    {
        yunSongFangShi = @"虚拟";
    }
    
    return yunSongFangShi;
}

NSString *getChengDanYunFei(NSString *chengDanYunFei)
{
    //谁承担运费.可选值:buyer(买家承担),seller(卖家承担运费).
    
    if ([chengDanYunFei isEqualToString: @"buyer"])
    {
        chengDanYunFei = @"买家承担";
    }else if ([chengDanYunFei isEqualToString: @"seller"])
    {
        chengDanYunFei = @"卖家承担运费";
    }else
    {
        chengDanYunFei = WUL_LIU_DETAIL_CELL_DEFAULT_TEXT;
    }
    
    return  chengDanYunFei;
}

NSString *getWuLiuDingDanZhuangTaiWitSign(NSString *sign_)
{
    /*
可选值:
     CREATED(订单已创建) 
     RECREATED(订单重新创建) 
     CANCELLED(订单已取消) 
     CLOSED(订单关闭) 
     SENDING(等候发送给物流公司) 
     ACCEPTING(已发送给物流公司,等待接单) 
     ACCEPTED(物流公司已接单) 
     REJECTED(物流公司不接单)
     PICK_UP(物流公司揽收成功) 
     PICK_UP_FAILED(物流公司揽收失败)
     LOST(物流公司丢单) 
     REJECTED_BY_RECEIVER(对方拒签) 
     ACCEPTED_BY_RECEIVER(对方已签收)
     */
    
    if ([sign_ isEqualToString: @"CREATED"])
    {
       sign_ =  @"订单已创建";
    }else if ([sign_ isEqualToString: @"RECREATED"])
    {
        sign_ =  @"订单重新创建";
    }else if ([sign_ isEqualToString: @"CANCELLED"])
    {
        sign_ =  @"订单已取消";
    }else if ([sign_ isEqualToString: @"CLOSED"])
    {
        sign_ =  @"订单关闭";
    }else if ([sign_ isEqualToString: @"SENDING"])
    {
        sign_ =  @"等候发送给物流公司";
    }else if ([sign_ isEqualToString: @"ACCEPTING"])
    {
        sign_ =  @"已发送给物流公司,等待接单";
    }else if ([sign_ isEqualToString: @"ACCEPTED"])
    {
        sign_ =  @"物流公司已接单";
    }else if ([sign_ isEqualToString: @"REJECTED"])
    {
        sign_ =  @"物流公司不接单";
    }else if ([sign_ isEqualToString: @"PICK_UP"])
    {
        sign_ =  @"物流公司揽收成功";
    }else if ([sign_ isEqualToString: @"PICK_UP_FAILED"])
    {
        sign_ =  @"物流公司揽收失败";
    }else if ([sign_ isEqualToString: @"LOST"])
    {
        sign_ =  @"物流公司丢单";
    }else if ([sign_ isEqualToString: @"REJECTED_BY_RECEIVER"])
    {
        sign_ =  @"对方拒签";
    }else if ([sign_ isEqualToString: @"ACCEPTED_BY_RECEIVER"])
    {
        sign_ =  @"对方已签收";
    }
    
    return sign_;
}














